package com.dms.modules.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.api.ApiResponse;
import com.dms.common.security.SecurityUtils;
import com.dms.modules.order.dto.RefundApplyDTO;
import com.dms.modules.order.entity.Order;
import com.dms.modules.order.service.OrderService;
import com.dms.modules.order.vo.OrderDetailVO;
import com.dms.modules.order.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * 用户订单控制器
 */
@RestController
@RequestMapping("/api/v1/user/orders")
@Api(tags = "用户订单管理")
@RequiredArgsConstructor
@PreAuthorize("hasRole('USER')")
public class UserOrderController {

    private final OrderService orderService;

    /**
     * 获取用户订单列表
     */
    @GetMapping
    @ApiOperation("获取用户订单列表")
    public ApiResponse<IPage<OrderVO>> getUserOrders(
            @ApiParam("页码") @RequestParam(defaultValue = "1") @Min(1) Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") @Min(1) Integer pageSize,
            @ApiParam("订单状态：0-待支付，1-已支付，2-已发货，3-已完成，4-已取消，5-退款中，6-已退款") 
            @RequestParam(required = false) Integer status) {
        
        Long userId = SecurityUtils.getCurrentUserId();
        Page<Order> page = new Page<>(pageNum, pageSize);
        IPage<OrderVO> orderVOPage = orderService.getUserOrders(page, userId, status);
        return ApiResponse.success(orderVOPage);
    }

    /**
     * 获取订单详情
     */
    @GetMapping("/{id}")
    @ApiOperation("获取订单详情")
    public ApiResponse<OrderDetailVO> getOrderDetail(@PathVariable @NotNull Long id) {
        Long userId = SecurityUtils.getCurrentUserId();
        OrderDetailVO orderDetailVO = orderService.getOrderDetail(userId, id);
        return ApiResponse.success(orderDetailVO);
    }

    /**
     * 确认收货
     */
    @PostMapping("/{id}/confirm")
    @ApiOperation("确认收货")
    public ApiResponse<Boolean> confirmReceipt(@PathVariable @NotNull Long id) {
        Long userId = SecurityUtils.getCurrentUserId();
        boolean result = orderService.confirmReceipt(userId, id);
        return ApiResponse.success(result);
    }

    /**
     * 申请退款
     */
    @PostMapping("/{id}/refund")
    @ApiOperation("申请退款")
    public ApiResponse<Long> applyRefund(
            @PathVariable @NotNull Long id,
            @RequestBody @Valid RefundApplyDTO refundApplyDTO) {
        Long userId = SecurityUtils.getCurrentUserId();
        Long refundId = orderService.applyRefund(userId, id, refundApplyDTO.getReason());
        return ApiResponse.success(refundId);
    }

    /**
     * 取消订单
     */
    @PostMapping("/{id}/cancel")
    @ApiOperation("取消订单")
    public ApiResponse<Boolean> cancelOrder(
            @PathVariable @NotNull Long id,
            @RequestParam(required = false) String reason) {
        Long userId = SecurityUtils.getCurrentUserId();
        boolean result = orderService.cancelOrder(userId, id, reason);
        return ApiResponse.success(result);
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除订单")
    public ApiResponse<Boolean> deleteOrder(@PathVariable @NotNull Long id) {
        Long userId = SecurityUtils.getCurrentUserId();
        boolean result = orderService.deleteOrder(userId, id);
        return ApiResponse.success(result);
    }
} 